package com.sunny.hp.common.util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.SimpleTimeZone;
import java.util.TimeZone;




/**
 * 클래스설명 :
 * @version : 2011. 3. 4.
 * @author : 신용천
 * @분류 :
 * eduterm / package com.comas.util;
 */

/**
 * 1. ClassName	:
 * 2. FileName		: DateUtil.java
 * 3. Package		: com.comas.util
 * 4. Commnet		:
 * 5. 작성자			: 신용천
 * 6. 작성일			: 2011. 3. 4. 오후 3:36:32 
 */
public class DateHelper {
    private int year;			/** 년도 */
    private int month;			/** 월 */
    private int day;			/** 일 */
    private int week;			/** 요일 */
    private int hour;			/** 시간 */
    private int minute;			/** 분 */
    private int second;			/** 초 */
    private int millisecond;	
    private int weekOfmonth;    /** 주차 */

    static int KORFLAG = 0;		/** 한글 */
    static int ENGFLAG = 1;		/** 영문 */

    private String firstDayFull="";         //인수로 받은 날짜가 속한 주의 첫일의 문자열
    private String lastDayFull="";          //인수로 받은 n주후의 날짜가 속한 주의 마지막일의 문자열

    Calendar todate;
    DecimalFormat dFormat = new DecimalFormat("00");

    Calendar temp_x;
    public DateHelper() {
        this.todate = Calendar.getInstance();
        this.temp_x = Calendar.getInstance();
        temp_x.set(Calendar.HOUR_OF_DAY, 0);
		 temp_x.set(Calendar.MINUTE, 0);
		 temp_x.set(Calendar.SECOND, 0);
        initialize();
    }
    
    public DateHelper(Calendar cal){
    	this.todate = cal;
    	 initialize();
    }
    
//    public static void main(String [] args){
//    	DateHelper date1 = new DateHelper(2013,5,25);
//    	//System.out.println(date1.getDay());
//    }
    
    public DateHelper(int year_, int month_, int date_) {
        this.todate = Calendar.getInstance();
        this.todate.set(year, month_, date_);
        initialize();
    }
    
    public DateHelper(int hour_, int minutes_, int second_, String s) {
        this.todate = Calendar.getInstance();
        this.todate.set(year, month, day, hour_, minutes_, second_);
        initialize();
    }

    public void initialize() {
        this.year = this.todate.get(Calendar.YEAR);
        this.month = this.todate.get(Calendar.MONTH) + 1;
        this.day = this.todate.get(Calendar.DATE);
        this.week = this.todate.get(Calendar.DAY_OF_WEEK);
        this.hour = this.todate.get(Calendar.HOUR_OF_DAY);
        this.minute = this.todate.get(Calendar.MINUTE);
        this.second = this.todate.get(Calendar.SECOND);
        this.millisecond = this.todate.get(Calendar.MILLISECOND);
        this.weekOfmonth = this.todate.get(Calendar.WEDNESDAY);
    }
    
    
    
    /**
     * 1. MethodName	: getTime
     * 2. ClassName		: DateHelper
     * 3. Commnet		: 현재 시스템의 시간을 반환한다.
     * 4. 작성자			: 신용천
     * 5. 작성일			: 2011. 3. 4. 오후 5:15:13
     * @return int
     * @return
     */
    public int getTime() {
        UtilHelper util = new UtilHelper();
        return Integer.parseInt(util.fillLeft(String.valueOf(this.hour), 2, "0") + util.fillLeft(String.valueOf(this.minute), 2, "0") + util.fillLeft(String.valueOf(this.second), 2, "0"));
    }
    
    /**
     * 1. MethodName	: getTime
     * 2. ClassName		: DateHelper
     * 3. Commnet		: 현재 시스템의 시간을 반환한다.
     * 4. 작성자			: 신용천
     * 5. 작성일			: 2011. 3. 4. 오후 5:15:13
     * @return int
     * @return
     */
    public String getTimeMinute() {
    	UtilHelper util = new UtilHelper();
        return util.fillLeft(String.valueOf(this.hour), 2, "0")  + util.fillLeft(String.valueOf(this.minute), 2, "0") ;
    }

    /**
     * 1. MethodName	: getTime
     * 2. ClassName		: DateHelper
     * 3. Commnet		: 현재 시스템의 시간을 반환한다.
     * 4. 작성자			: 신용천
     * 5. 작성일			: 2011. 3. 4. 오후 3:39:22
     * @return String
     * @param delim
     * @return
     */
    public String getTime(String delim) {
        UtilHelper util = new UtilHelper();
        return util.fillLeft(String.valueOf(this.hour), 2, "0") + delim + util.fillLeft(String.valueOf(this.minute), 2, "0") + delim + util.fillLeft(String.valueOf(this.second), 2, "0");
    }


    /**
     * 1. MethodName	: getDate
     * 2. ClassName		: DateHelper
     * 3. Commnet		:
     * 4. 작성자			: 신용천
     * 5. 작성일			: 2011. 3. 14. 오후 3:00:59
     * @return String
     * @param delim
     * @return
     */
    public String getStrDate() {
        String stYear 	= String.valueOf( this.year );
        String stMonth	= String.valueOf( this.month );
        String stDay	= String.valueOf( this.day );
        String stDate	= "";

        if( stMonth.length() == 1 ){
            stMonth = "0" + stMonth;
        }

        if( stDay.length() == 1 ){
            stDay = "0" + stDay;
        }

        stDate = stYear + stMonth + stDay;

        return stDate;
    }
    
    /**
     * 1. MethodName	: getDate
     * 2. ClassName		: DateHelper
     * 3. Commnet		:
     * 4. 작성자			: 신용천
     * 5. 작성일			: 2011. 3. 14. 오후 3:00:59
     * @return String
     * @param delim
     * @return
     */
    public String getStrDateTime() {
        String stYear 	= String.valueOf( this.year );
        String stMonth	= String.valueOf( this.month );
        String stDay	= String.valueOf( this.day );
        String stHour	= String.valueOf( this.hour );
        String stMinute	= String.valueOf( this.minute );
        String stDate	= "";


        if( stMonth.length() == 1 ){
            stMonth = "0" + stMonth;
        }

        if( stDay.length() == 1 ){
            stDay = "0" + stDay;
        }
        
        if( stHour.length() == 1 ){
        	stHour = "0" + stHour;
        }
        
        if( stMinute.length() == 1 ){
        	stMinute = "0" + stMinute;
        }

        stDate = stYear + stMonth + stDay + stHour + stMinute;

        return stDate;
    }

    public String getStrDate(String delim) {
        String stYear 	= String.valueOf( this.year );
        String stMonth	= String.valueOf( this.month );
        String stDay	= String.valueOf( this.day );
        String stDate	= "";

        if( stMonth.length() == 1 ){
            stMonth = "0" + stMonth;
        }

        if( stDay.length() == 1 ){
            stDay = "0" + stDay;
        }

        stDate = stYear + delim + stMonth + delim + stDay;

        return stDate;
    }
    
    public String getStrMonthDay(String delim){
    	return this.month + delim + this.day;
    }
    
    /**
     * 1. MethodName	: getStrDateTimeSecond
     * 2. ClassName		: DateHelper
     * 3. Commnet		: 현재 시스템의 시간을 반환한다.(초단위 포함)
     * 4. 작성자			: 신용천
     * 5. 작성일			: 2011. 3. 4. 오후 3:39:22
     * @return String
     * @param delim
     * @return
     */
    public String getStrDateTimeSecond() {
        UtilHelper util = new UtilHelper();
        return util.fillLeft(String.valueOf(this.year), 4, "0") 
        		+ util.fillLeft(String.valueOf(this.month), 2, "0") 
        		+ util.fillLeft(String.valueOf(this.day), 2, "0") 
        		+ util.fillLeft(String.valueOf(this.hour), 2, "0") 
        		+ util.fillLeft(String.valueOf(this.minute), 2, "0") 
        		+ util.fillLeft(String.valueOf(this.second), 2, "0");
    }
    
    public String getStrDateTimeSecondMillisecond() {
        UtilHelper util = new UtilHelper();
        return util.fillLeft(String.valueOf(this.year), 4, "0") 
        		+ util.fillLeft(String.valueOf(this.month), 2, "0") 
        		+ util.fillLeft(String.valueOf(this.day), 2, "0") 
        		+ util.fillLeft(String.valueOf(this.hour), 2, "0") 
        		+ util.fillLeft(String.valueOf(this.minute), 2, "0") 
        		+ util.fillLeft(String.valueOf(this.second), 2, "0")
        		+ util.fillLeft(String.valueOf(this.millisecond), 3, "0");
    }
    
    
    /**
     * 1. MethodName	: getStrDateTimeSecond
     * 2. ClassName		: DateHelper
     * 3. Commnet		: 현재 시스템의 시간을 반환한다.(초단위 포함)
     * 4. 작성자			: 신용천
     * 5. 작성일			: 2011. 3. 4. 오후 3:39:22
     * @return String
     * @param delim
     * @return
     */
    public String getStrDateTimeSecond(String delim1, String delim2) {
        UtilHelper util = new UtilHelper();
        return util.fillLeft(String.valueOf(this.year), 4, "0") 
        		 + delim1
        		+ util.fillLeft(String.valueOf(this.month), 2, "0") 
        		 + delim1
        		+ util.fillLeft(String.valueOf(this.day), 2, "0") 
        		 + " " 
        		+ util.fillLeft(String.valueOf(this.hour), 2, "0") 
        		 + delim2
        		+ util.fillLeft(String.valueOf(this.minute), 2, "0") 
        		 + delim2
        		+ util.fillLeft(String.valueOf(this.second), 2, "0");
    }

	public String addDate(int amount) {
		return addDate(amount, "");
	}

	public String addDate(int amount, String delim) {
		FormatHelper format = new FormatHelper();

		Calendar temp = Calendar.getInstance();
		temp.set(this.year, this.month - 1, this.day);
		temp.add(Calendar.DATE, amount);

		int year = temp.get(Calendar.YEAR);
		int month = temp.get(Calendar.MONTH) + 1;
		int day = temp.get(Calendar.DATE);

		return format.dateFormat(year, month, day, delim);
	}

	public String addMonth(int amount, String delim) {
		FormatHelper format = new FormatHelper();

		Calendar temp = Calendar.getInstance();
		temp.set(this.year, this.month - 1, this.day);
		temp.add(Calendar.MONTH, amount);

		int year = temp.get(Calendar.YEAR);
		int month = temp.get(Calendar.MONTH) + 1;
		int day = temp.get(Calendar.DATE);

		return format.dateFormat(year, month, day, delim);
	}

    public String addTime(int amount) {
        FormatHelper format  = new FormatHelper();

        Calendar temp = Calendar.getInstance();
        temp.set(this.year, this.month-1, this.day, this.hour, this.minute, this.second);
        temp.add(Calendar.HOUR, amount);
        int thour  =	temp.get(Calendar.HOUR);
        int tminute = temp.get(Calendar.MINUTE) ;
        int tsecond   = temp.get(Calendar.SECOND);


        return format.timeFormat(thour, tminute, tsecond, "");

    }
    
    public String addMinutes(int amount) {
    	
    	//System.out.println("amount ==> " + amount);
    	StringBuffer retValue = new StringBuffer();

		Calendar temp = Calendar.getInstance();
		temp.set(this.year, this.month - 1, this.day, this.hour, this.minute, this.second);
		temp.add(Calendar.MINUTE, amount);

        int thour  =	temp.get(Calendar.HOUR);
        int tminute = temp.get(Calendar.MINUTE) ;
        
//        this.hour = thour;
//        this.minute = tminute;


		UtilHelper util = new UtilHelper();
		retValue.append(util.fillLeft(thour + "", 2, "0"));
		retValue.append(util.fillLeft(tminute + "", 2, "0"));
		retValue.append("00");

		return retValue.toString();

    }
    
	public String addDateTime(int amount) {
		StringBuffer retValue = new StringBuffer();

		Calendar temp = Calendar.getInstance();
		temp.set(this.year, this.month - 1, this.day, this.hour, this.minute, this.second);
		temp.add(Calendar.HOUR, amount);

		int year = temp.get(Calendar.YEAR);
		int month = temp.get(Calendar.MONTH) + 1;
		int day = temp.get(Calendar.DATE);
		int thour = temp.get(Calendar.HOUR_OF_DAY);

		UtilHelper util = new UtilHelper();
		retValue.append(year);
		retValue.append(util.fillLeft(month + "", 2, "0"));
		retValue.append(util.fillLeft(day + "", 2, "0"));
		retValue.append(util.fillLeft(thour + "", 2, "0"));

		return retValue.toString();
	}

    public String getYear(){
        return String.valueOf( this.year );
    }
    
    public int getYearInt(){
    	return this.year;
    }
    public String getMonth(){
        String stMonth  = String.valueOf( this.month );
        if( stMonth.length() == 1 ){
            stMonth = "0" + stMonth;
        }
        return stMonth;
    }
    
    public int getMonthInt(){
    	return this.month;
    }

    public String getDay(){
        String stDay    = String.valueOf( this.day );
        if( stDay.length() == 1 ){
            stDay = "0" + stDay;
        }
        return stDay;
    }
    
    public int getDayInt(){
    	return this.day;
    }

    public String getHour(){
        UtilHelper util = new UtilHelper();
        return util.fillLeft(String.valueOf(this.hour), 2, "0");
    }
    
    public int getHourInt(){
    	return this.hour;
    }
    
    public String getMinute(){
        UtilHelper util = new UtilHelper();
        return util.fillLeft(String.valueOf(this.minute), 2, "0");
    }
    
    public int getMinuteInt(){
    	return this.minute;
    }
    
    public int getWeekOfMonth(){
    	return this.weekOfmonth;
    }
    
    public int getWeekInt(){
    	return this.week;
    }
    
    public Calendar getTodate(){
    	return todate;
    }
    public String getWeek(){
    	String weekStr = "";
    	switch (this.week){
        case 1:
        	weekStr = "일요일";
            break;
        case 2:
        	weekStr = "월요일";
            break;
        case 3:
        	weekStr = "화요일";
            break;
        case 4:
        	weekStr = "수요일";
            break;
        case 5:
        	weekStr = "목요일";
            break;
        case 6:
        	weekStr = "금요일";
            break;
        case 7:
        	weekStr = "토요일";
            break;
        }
    	
    	return weekStr;

    }
    
    public long diffOfDate(String begin) throws Exception
    {
      SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
   
      Date beginDate = formatter.parse(begin);
      Date endDate = formatter.parse(getStrDate());
   
      long diff = endDate.getTime() - beginDate.getTime();
      long diffDays = diff / (24 * 60 * 60 * 1000);
   
      return diffDays;
    }

	public static ArrayList<String> getGapDateList(String fromDate, String toDate){
		//시작 , 끝 날짜 임의 세팅

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

		  Calendar cal = Calendar.getInstance();

		  try {
		   cal.setTime(sdf.parse(fromDate));
		  } catch (Exception e) {
		  }

		  int count = getDiffDayCount(fromDate, toDate);

		  // 시작일부터
		  cal.add(Calendar.DATE, -1);

		  // 데이터 저장
		  ArrayList<String> list = new ArrayList<String>();

		  for (int i = 0; i <= count; i++) {
		   cal.add(Calendar.DATE, 1);

		   list.add(sdf.format(cal.getTime()));
		  }

		  String[] result = new String[list.size()];

		  list.toArray(result);
		
		return list;
	}
	
	/**
	  * 두날짜 사이의 일수를 리턴
	  * @param fromDate yyyyMMdd 형식의 시작일
	  * @param toDate yyyyMMdd 형식의 종료일
	  * @return 두날짜 사이의 일수
	  */
	 public static int getDiffDayCount(String fromDate, String toDate) {
	  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

	  try {
	   return (int) ((sdf.parse(toDate).getTime() - sdf.parse(fromDate)
	     .getTime()) / 1000 / 60 / 60 / 24);
	  } catch (Exception e) {
	   return 0;
	  }
	 }
	 public String addDateAllTime(int addMinute){
		 
			
			StringBuffer retValue = new StringBuffer();


			UtilHelper util = new UtilHelper();
			
			temp_x.add(Calendar.MINUTE, addMinute);
			retValue = new StringBuffer();
			retValue.append(util.fillLeft(temp_x.get(Calendar.HOUR_OF_DAY) + "", 2, "0"));
			retValue.append(util.fillLeft(temp_x.get(Calendar.MINUTE) + "", 2, "0"));
			retValue.append("00");
			
			return retValue.toString();
	 }
	 
	 public static int getDiffMinuteSecond(Calendar postCal){

		 long startTime = postCal.getTimeInMillis();

		 long endTime = System.currentTimeMillis();
		 
		 long elapseTime = endTime-startTime;

		 return Integer.parseInt(getTime(elapseTime));

	 }
	 
	 public static String getTime(long diffTime){
			
			long ss = diffTime/1000;
			long mm = ss/60;
			long HH = mm/60;

			String rSs = getTimeString(ss);
			String rMm = getTimeString(mm);
			String rHH = getTimeString(HH);

			return rHH +rMm +rSs;
		}
		public static String getTimeString(long time){

			String str;

			while(true){
				if(time>=60){
					time = time - 60;
				}else{
					break;
				}
			}

			str = String.valueOf(time);
			if(str.length()==1){
				str = "0" + str;
			}

			return str;
		}
		
		public static String currentDate(String format)
		{
			java.util.Date sysdate = new java.util.Date(System.currentTimeMillis());
			String toDayDate	   = new java.text.SimpleDateFormat(format).format(sysdate); 
			return toDayDate;
		}
		
		public static String getTimeFileName(String cate)
		{
			long currentTime      = System.currentTimeMillis();
			SimpleDateFormat sdf  = new SimpleDateFormat("yyyyMMddHHmmss");
			return cate+sdf.format(new Date(currentTime)); 
			
		}		
}
